package com.liu.servlet;

import javax.servlet.ServletOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * @author LIU
 * @version 1.0
 * @description TODO
 * @date 2024/2/6 10:13
 */
public class Response extends AbstractHttpServletResponse {
    private byte SP = ' ';
    private byte CR = '\r';
    private byte LF = '\n';


    /**  请求状态码  */
    private int status = 200;
    /**  请求信息  */
    private String message = "OK";
    /**  请求头  */
    private Map<String, String> headers = new HashMap<>();

    /**  存储的响应体 */
    private ResponseServletOutputStream servletOutputStream = new ResponseServletOutputStream();

    /**  请求 */
    private Request request;
    /**  socket响应流 */
    private OutputStream socketOutputStream;

    public Response(Request request) {
        this.request = request;

        try {
            this.socketOutputStream = request.getSocket().getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void setStatus(int status) {
        this.status = status;
    }

    @Override
    public int getStatus() {
        return status;
    }

    @Override
    public void setStatus(int status, String message) {
        this.status = status;
        this.message = message;
    }


    @Override
    public void addHeader(String key, String val) {
        headers.put(key, val);
    }
    /**
     * @description TODO
     * @author liuzq
     * @date 2024/2/6 11:16
     * @return ServletOutputStream  响应体
     */
    @Override
    public ServletOutputStream getOutputStream() throws IOException {
        return servletOutputStream;
    }

    /**
     * 发送响应
     */
    public void complete() throws IOException {
        // 响应行
        sendResponseLine();
        // 响应头
        sendResponseHeader();
        // 响应体
        sendResponseBody();

    }

    private void sendResponseLine() throws IOException {
        socketOutputStream.write(request.getProtocol().getBytes());
        socketOutputStream.write(SP);
        socketOutputStream.write(status);
        socketOutputStream.write(SP);
        socketOutputStream.write(message.getBytes(StandardCharsets.UTF_8));
        socketOutputStream.write(CR);
        socketOutputStream.write(LF);
    }

    private void sendResponseHeader() throws IOException {
        if (!headers.containsKey("Content-Type"))
            headers.put("Content-Type","text/plain;charset=utf-8");
        if (!headers.containsKey("Content-Length"))
            headers.put("Content-Length",String.valueOf(servletOutputStream.getPos()));


        for (Map.Entry<String, String> entry : headers.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();

            socketOutputStream.write(key.getBytes());
            socketOutputStream.write(":".getBytes());
            socketOutputStream.write(value.getBytes());
            socketOutputStream.write(CR);
            socketOutputStream.write(LF);
        }
        socketOutputStream.write(CR);
        socketOutputStream.write(LF);
    }


    private void sendResponseBody() throws IOException {
        socketOutputStream.write(servletOutputStream.getBytes());
    }
}
